#include <iostream>
#include <signal.h>
#include <vector>
#include <unistd.h>

using namespace std;

void header(int sig)
{
    cout << sig << "号信号已经被递达!!" << endl;
}

void print_signal(const sigset_t &pending)
{
    for (size_t i = 31; i >= 1; i--)
    {
        if (sigismember(&pending, i))
        {
            cout << "1";
        }
        else
        {
            cout << "0";
        }
    }
    cout << endl;
}

int main()
{
    vector<int> block_sig{2, 3};
    sigset_t block, oblock, pending;
    sigemptyset(&block);
    sigemptyset(&oblock);
    for (const auto &sig : block_sig)
    {
        sigaddset(&block, sig);
        signal(sig, header);
    }
    sigprocmask(SIG_SETMASK, &block, &oblock);
    int cnt = 10;
    while (true)
    {
        sigemptyset(&pending);
        sigpending(&pending);
        print_signal(pending);
        sleep(1);
        if (cnt-- == 0)
        {
            sigprocmask(SIG_SETMASK, &oblock, &block);
            cout << "恢复对信号的屏蔽，不屏蔽任何信号\n";
        }
    }

    return 0;
}